﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace WallCreateAndEdit.WallModule.EditWall
{
	public partial class ChangeOutWallTypeForm : System.Windows.Forms.Form
	{
		public ChangeOutWallTypeForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.InitializeWallTypeList();
		}

		public bool SelectOutWall
		{
			get
			{
				return this.rdbSelect.Checked;
			}
		}

		public WallType SelectWallType
		{
			get
			{
				if (this.cmbWallTypeList.SelectedIndex != -1)
				{
					foreach (WallType wallType in this.m_WallTypeList)
					{
						if (wallType.Name.CompareTo(this.cmbWallTypeList.Text) == 0)
						{
							return wallType;
						}
					}
				}
				return null;
			}
		}

		private void InitializeWallTypeList()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(WallType)).ToElements();
			foreach (Element element in list)
			{
				WallType wallType = element as WallType;
				if (wallType != null && wallType.Kind == null)
				{
					this.m_WallTypeList.Add(wallType);
					this.cmbWallTypeList.Items.Add(wallType.Name);
				}
			}
		}

		private void ChangeOutWallTypeForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void ChangeOutWallTypeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveCompornentData();
		}

		private void LoadCompornentData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "ChangeOutWallTypeForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				if (this.cmbWallTypeList.Items.Count > 0)
				{
					this.cmbWallTypeList.SelectedIndex = 0;
				}
				this.rdbAll.Checked = true;
				this.rdbSelect.Checked = false;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num < this.cmbWallTypeList.Items.Count)
			{
				this.cmbWallTypeList.SelectedIndex = num;
			}
			this.rdbAll.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.rdbSelect.Checked = Convert.ToBoolean(streamReader.ReadLine());
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
                base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "ChangeOutWallTypeForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbWallTypeList.SelectedIndex);
			streamWriter.WriteLine(this.rdbAll.Checked);
			streamWriter.WriteLine(this.rdbSelect.Checked);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private ExternalCommandData m_Revit;

		private List<WallType> m_WallTypeList = new List<WallType>();
	}
}
